﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Threading.Tasks;
using Encrypt;
using Microsoft.AspNetCore.Mvc;
using Model;
using Newtonsoft.Json;

namespace EncryptWebClient.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        // GET api/values
        [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {
            var client =new HttpClient();
            var url = "http://localhost:5000/api/vipmodify";
            string result = null;
            try
            {
                var json=JsonConvert.SerializeObject(new VipInfo() { Code = "client", Name = "客户端" });
                var enJson = Encrypt.AES.AesEncrypt(json, AES.SimpleKey);
                using (var content=new StringContent(enJson))
                {
                    content.Headers.ContentType=new MediaTypeHeaderValue("application/json");
                    content.Headers.Add("Encrypt", "1");
                    content.Headers.Add("ApiVer", "2.0");
                    var res = client.PostAsync(url, content).Result;
                   var resrawbyte = res.Content.ReadAsStringAsync().Result;
                    result = Encrypt.AES.AesDecrypt(resrawbyte, AES.SimpleKey);
                } 
            }
            catch (TaskCanceledException taskException)
            {
                //超时异常
                result = taskException.Message;
            }
            catch (Exception c)
            {
                result = c.Message;
            }
            return new string[] { "value1", "value2", result };
        }

        // GET api/values/5
        [HttpGet("{id}")]
        public ActionResult<string> Get(int id)
        {
            return "value";
        }

        // POST api/values
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }

        // PUT api/values/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/values/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
